﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LawProject.Models;
using Repository.Repositories;
using Repository.Entities;
using Repository.Contracts;
using Core.Interfaces;
using System.IO;

namespace LawProject.Controllers
{
    public class AdminController : Controller
    {
        private IRepository<TBLinhVuc> linhvucRepository;
        private IRepository<TBBanTin> bantinRepository;
        private IRepository<TBBanTinCon> bantinconRepository;
        private IRepository<TBHoTro> hotroRepository;
        private IRepository<TBTinMoi> tinmoiRepository;
        private IRepository<TBTTC> ttcRepository;
        private IRepository<TBUser> userRepository;

        public AdminController()
            : this(IoC.Resolve<IRepository<TBLinhVuc>>(), IoC.Resolve<IRepository<TBBanTin>>(), IoC.Resolve<IRepository<TBBanTinCon>>(), IoC.Resolve<IRepository<TBHoTro>>(), IoC.Resolve<IRepository<TBTinMoi>>(), IoC.Resolve<IRepository<TBTTC>>(), IoC.Resolve<IRepository<TBUser>>())
        {
        }

        public AdminController(IRepository<TBLinhVuc> linhvucRepository, IRepository<TBBanTin> bantinRepository, IRepository<TBBanTinCon> bantinconRepository, IRepository<TBHoTro> hotroRepository, IRepository<TBTinMoi> tinmoiRepository, IRepository<TBTTC> ttcRepository, IRepository<TBUser> userRepository)
        {
            this.linhvucRepository = linhvucRepository;
            this.bantinconRepository = bantinconRepository;
            this.bantinRepository = bantinRepository;
            this.hotroRepository = hotroRepository;
            this.tinmoiRepository = tinmoiRepository;
            this.ttcRepository = ttcRepository;
            this.userRepository = userRepository;
        }

        public ActionResult Index()
        {
            Session["lang"] = "xxx";
            List<LinhVucClass> resultMenu = new List<LinhVucClass>();
            List<TBLinhVuc> menudata = linhvucRepository.GetSome(a => a.MaLinhVuc != "").ToList();
            foreach (var item in menudata)
            {
                LinhVucClass lv = new LinhVucClass { MaLinhVuc = item.MaLinhVuc, TenLinhVuc = item.TenLinhVucVN, Loai = item.Loai };
                resultMenu.Add(lv);
            }
            return View(resultMenu);
        }

        public ActionResult ChiTietLinhVuc(string malv)
        {            
            TBLinhVuc linhvuc = linhvucRepository.GetSome(a=>a.MaLinhVuc==malv).FirstOrDefault();
            List<TBBanTin> bantinData = bantinRepository.GetSome(a => a.MaLinhVuc == malv).ToList();
            ListBanTin resultData = new ListBanTin();
            resultData.TenLinhVuc = linhvuc.TenLinhVucVN;
            resultData.MaLinhVuc = linhvuc.MaLinhVuc.ToString();
            foreach (var item in bantinData)
            {
                SortBanTin sort = new SortBanTin();
                sort.MaBanTin = item.MaBanTin.ToString();
                sort.Title = item.TieuDeVN;
                resultData.ListData.Add(sort);
            }
            return View(resultData);
        }

        public ActionResult ChiTietBanTin(string mabt)
        {
            var bantin = bantinRepository.GetSome(a => a.MaBanTin == new Guid(mabt)).First();
            BanTinTemplate tinTemplate = new BanTinTemplate { MaBanTin = bantin.MaBanTin.ToString(), MaThuMucCha = bantin.MaLinhVuc, TieuDe = bantin.TieuDeVN, TomTat = bantin.TomTatVN, NoiDung = bantin.NoiDungVN, TieuDeEN=bantin.TieuDeEN, TomTatEN = bantin.TomTatEN, NoiDungEN=bantin.NoiDungEN, NguoiDT = bantin.NguoiDT };
            var listBTC = bantinconRepository.GetSome(a => a.MaBanTin == new Guid(mabt));
            BanTinContract result = new BanTinContract();
            result.ChiTiet = tinTemplate;
            foreach (var item in listBTC)
            {
                SortBanTinCon sort = new SortBanTinCon { MaBanTinCon = item.MaBTC.ToString(), Title = item.TieuDeVN };
                result.ListData.Add(sort);
            }
            return View(result);
        }



        public ActionResult BanTinCreate(string malv)
        {
            BanTinClass bantin = new BanTinClass();
            bantin.MaLinhVuc = malv;
            return View(bantin);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult BanTinCreate(BanTinClass bantin)
        {
            TBBanTin newObject = new TBBanTin();
            newObject.MaBanTin = Guid.NewGuid();
            newObject.MaLinhVuc = bantin.MaLinhVuc;
            newObject.TieuDeVN = bantin.TieuDe_VN;
            newObject.TieuDeEN = bantin.TieuDe_EN;
            newObject.TomTatVN = bantin.TomTat_VN;
            newObject.TomTatEN = bantin.TomTat_EN;
            newObject.NoiDungVN = bantin.NoiDung_VN;
            newObject.NoiDungEN = bantin.NoiDung_EN;
            newObject.NgayDT = DateTime.Now;
            bantinRepository.Save(null, newObject);
            TBTinMoi tinmoi = new TBTinMoi { MaTinMoi = newObject.MaBanTin, TieuDeVN = newObject.TieuDeVN, TieuDeEN = newObject.TieuDeEN, NgayDT = DateTime.Now, Loai = true };
            tinmoiRepository.Save(null, tinmoi);
            return RedirectToAction("ChiTietLinhVuc", "Admin", new { malv = bantin.MaLinhVuc });
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult BanTinUpdate(BanTinContract bantin)
        {
            var bantindatabase = bantinRepository.GetOne(a=>a.MaBanTin == new Guid(bantin.ChiTiet.MaBanTin));
            TBBanTin newBt = new TBBanTin { MaBanTin = new Guid(bantin.ChiTiet.MaBanTin), MaLinhVuc = bantin.ChiTiet.MaThuMucCha, TieuDeVN = bantin.ChiTiet.TieuDe, TomTatVN = bantin.ChiTiet.TomTat, NoiDungVN = bantin.ChiTiet.NoiDung, TieuDeEN = bantin.ChiTiet.TieuDeEN, TomTatEN = bantin.ChiTiet.TomTatEN, NoiDungEN = bantin.ChiTiet.NoiDungEN, NguoiDT = bantin.ChiTiet.NguoiDT, NgayDT = DateTime.Now };
            bantinRepository.Save(bantindatabase, newBt);
            return RedirectToAction("ChiTietBanTin", new { mabt = newBt.MaBanTin.ToString() });
        }

        public ActionResult BanTinDelete(string mabt)
        {
            var banTinConList = bantinconRepository.GetSome(a => a.MaBanTin == new Guid(mabt)).ToList();
            foreach (var item in banTinConList)
            {
                bantinconRepository.Delete(item);
            }
            var bantin = bantinRepository.GetSome(a => a.MaBanTin == new Guid(mabt)).FirstOrDefault();
            if (bantin != null)
            {
                bantinRepository.Delete(bantin);
                var newobj = tinmoiRepository.GetOne(a => a.MaTinMoi == bantin.MaBanTin);
                if (newobj != null)
                    tinmoiRepository.Delete(newobj);
                return RedirectToAction("ChiTietLinhVuc", new { malv = bantin.MaLinhVuc });

            }
            else
                return View();
        }

        public ActionResult BanTinConCreate(string mabt)
        {
            BanTinConClass bantincon = new BanTinConClass();
            bantincon.MaBanTin = mabt;
            return View(bantincon);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult BanTinConCreate(BanTinConClass bantin)
        {

            TBBanTinCon newObject = new TBBanTinCon();
            newObject.MaBTC = Guid.NewGuid();
            newObject.MaBanTin = new Guid(bantin.MaBanTin);
            newObject.TieuDeVN = bantin.TieuDe_VN;
            newObject.TieuDeEN = bantin.TieuDe_EN;
            newObject.TomTatVN = bantin.TomTat_VN;
            newObject.TomTatEN = bantin.TomTat_EN;
            newObject.NoiDungVN = bantin.NoiDung_VN;
            newObject.NoiDungEN = bantin.NoiDung_EN;
            newObject.NgayDT = DateTime.Now;
            bantinconRepository.Save(null, newObject);
            return RedirectToAction("ChiTietBanTin", "Admin", new {mabt = bantin.MaBanTin});
        }

        public ActionResult ChiTietBanTinCon(string mabtc)
        {
            var bantincon = bantinconRepository.GetSome(a => a.MaBTC == new Guid(mabtc)).FirstOrDefault();
            BanTinTemplate tinTemplate = new BanTinTemplate { MaBanTin = bantincon.MaBanTin.ToString(), MaThuMucCha = bantincon.MaBanTin.ToString(), TieuDe = bantincon.TieuDeVN, TomTat = bantincon.TomTatVN, NoiDung = bantincon.NoiDungVN, TieuDeEN = bantincon.TieuDeEN, TomTatEN = bantincon.TomTatEN, NoiDungEN = bantincon.NoiDungEN, NguoiDT = bantincon.NguoiDT };
            return View(tinTemplate);
        }

        public ActionResult BanTinConDelete(string mabtc)
        {
            var bantincon = bantinconRepository.GetSome(a => a.MaBTC == new Guid(mabtc)).FirstOrDefault();
            bantinconRepository.Delete(bantincon);
            var ttc = ttcRepository.GetOne(a => a.MaTT == "tc");
            int count = int.Parse(ttc.NoiDungEN);
            count--;
            TBTTC newttc = new TBTTC { MaTT = ttc.MaTT, TenTT = ttc.TenTT, TenTTEN = ttc.TenTTEN, NoiDung = ttc.NoiDung, NoiDungEN = count.ToString() };
            ttcRepository.Save(ttc, newttc);
            return RedirectToAction("ChiTietBanTin", "Admin", new { mabt = bantincon.MaBanTin.ToString() });
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult BanTinConUpdate(BanTinTemplate bantin)
        {
            var bantindatabase = bantinconRepository.GetOne(a => a.MaBTC == new Guid(bantin.MaBanTin));
            TBBanTinCon newBt = new TBBanTinCon { MaBTC = new Guid(bantin.MaBanTin), MaBanTin = new Guid(bantin.MaThuMucCha), TieuDeVN = bantin.TieuDe, TomTatVN = bantin.TomTat, NoiDungVN = bantin.NoiDung, TieuDeEN = bantin.TieuDeEN, TomTatEN = bantin.TomTatEN, NoiDungEN = bantin.NoiDungEN, NguoiDT = bantin.NguoiDT, NgayDT = DateTime.Now };
            bantinconRepository.Save(bantindatabase, newBt);
            return RedirectToAction("ChiTietBanTinCon", new { mabtc = newBt.MaBTC.ToString() });
        }

        public ActionResult ChiTietTTC(string mattc)
        {
            var ttc = ttcRepository.GetOne(a => a.MaTT == mattc);
            ViewTTC result = new ViewTTC { MaTT = ttc.MaTT, TenTT = ttc.TenTT, TenEN = ttc.TenTTEN, NoiDung = ttc.NoiDung, NoiDungEN = ttc.NoiDungEN };
            return View(result);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThongTinUpdate(ViewTTC ttc)
        {
            var ogr = ttcRepository.GetOne(a => a.MaTT == ttc.MaTT);
            TBTTC newObj = new TBTTC();
            newObj.MaTT = ttc.MaTT;
            newObj.TenTT = ttc.TenTT;
            newObj.TenTTEN = ttc.TenEN;
            newObj.NoiDung = ttc.NoiDung;
            newObj.NoiDungEN = ttc.NoiDungEN;
            ttcRepository.Save(ogr, newObj);
            return RedirectToAction("ChiTietTTC", "Admin", new { mattc = ttc.MaTT });

        }





    }
}
